package com.iov.failurelog.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.iov.failurelog.controller.HbaseController;
import com.iov.failurelog.utils.PoolDemoThread;
import com.iov.failurelog.vo.VehicleMsgEntity;
import com.rabbitmq.client.Channel;
import org.apache.avro.data.Json;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

import static cn.hutool.core.thread.GlobalThreadPool.submit;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2023/11/28 9:58
 */

@Component
public class HbaseMq {

    @Resource
    private HbaseController hbase;

    @Resource
    private PoolDemoThread poolDemoThread;


    // 信息全部存入hbase

    @RabbitListener(queues = {"queue2"})
    public void hbaseSave(Message message, Channel channel) throws IOException {
            poolDemoThread.thread();
            submit(() -> {
                byte[] body = message.getBody();
                String msg = new String(body);

                int i = msg.indexOf("{");
                String substring = msg.substring(i);
                VehicleMsgEntity vehicleMsgEntity = JSON.parseObject(substring, VehicleMsgEntity.class);
                // 获取车辆vin
                String vin = vehicleMsgEntity.getVin();

                try {
                    hbase.insertUpdate(JSON.toJSONString(vehicleMsgEntity), String.valueOf(System.currentTimeMillis()));
                    System.out.println("车辆监控插入实时数据成功!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
